package com.ruoyi.project.WebMoudle.streamTerrace.controller;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.project.WebMoudle.terraceDetail.domain.TerraceDetail;
import com.ruoyi.project.WebMoudle.terraceDetail.service.ITerraceDetailService;
import com.ruoyi.project.WebMoudle.useTerms.domain.UseTerms;
import com.ruoyi.project.WebMoudle.useTerms.service.IUseTermsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.WebMoudle.streamTerrace.domain.StreamTerrace;
import com.ruoyi.project.WebMoudle.streamTerrace.service.IStreamTerraceService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 平台收入Controller
 * 
 * @author ruoyi
 * @date 2020-08-15
 */
@Controller
@RequestMapping("/streamTerrace/streamTerrace")
public class StreamTerraceController extends BaseController
{
    private String prefix = "streamTerrace/streamTerrace";

    @Autowired
    private IStreamTerraceService streamTerraceService;

    @Autowired
    private IUseTermsService useTermsService;

    @Autowired
    private ITerraceDetailService terraceDetailService;


    @GetMapping()
    public String streamTerrace()
    {
        return prefix + "/streamTerrace";
    }

    /**
     * 查询平台收入列表
     */

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StreamTerrace streamTerrace)
    {
        startPage();
        List<StreamTerrace> list = streamTerraceService.selectStreamTerraceList(streamTerrace);
        return getDataTable(list);
    }

    /**
     * 导出平台收入列表
     */

    @Log(title = "平台收入", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StreamTerrace streamTerrace)
    {
        List<StreamTerrace> list = streamTerraceService.selectStreamTerraceList(streamTerrace);
        ExcelUtil<StreamTerrace> util = new ExcelUtil<StreamTerrace>(StreamTerrace.class);
        return util.exportExcel(list, "streamTerrace");
    }

    /**
     * 新增平台收入
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存平台收入
     */

    @Log(title = "平台收入", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StreamTerrace streamTerrace)
    {
        return toAjax(streamTerraceService.insertStreamTerrace(streamTerrace));
    }

    /**
     * 修改平台收入
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        StreamTerrace streamTerrace = streamTerraceService.selectStreamTerraceById(id);
        mmap.put("streamTerrace", streamTerrace);
        return prefix + "/edit";
    }

    /**
     * 修改保存平台收入
     */

    @Log(title = "平台收入", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StreamTerrace streamTerrace)
    {
        return toAjax(streamTerraceService.updateStreamTerrace(streamTerrace));
    }

    /**
     * 删除平台收入
     */

    @Log(title = "平台收入", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(streamTerraceService.deleteStreamTerraceByIds(ids));
    }


    /*修改平台金额页面*/
    @RequiresPermissions("sysEditMoney:sysEditMoney")
    @GetMapping("/sysEditMoney")
    public String sysEditMoney1(ModelMap mmap)
    {
        UseTerms useTerms = useTermsService.selectUseTermsList(null).get(0);

        mmap.put("terraceMoney",useTerms.getTerraceMoney());
        return "updateTerraceMoney";
    }

    /*修改平台金额数据*/
    @RequiresPermissions("sysEditMoney:sysEditMoney")
    @PostMapping("/sysEditMoney")
    @ResponseBody
    @Transactional
    public AjaxResult sysEditMoney2(BigDecimal varietyValue,BigDecimal nowValue,String operateType)
    {
        if (operateType.equals("2")){   //提现
            varietyValue = varietyValue.negate();
        }
        TerraceDetail terraceDetail = new TerraceDetail();
        terraceDetail.setOperateType(operateType);
        terraceDetail.setOperateMoney(varietyValue);
        terraceDetail.setHandleName(getLoginName());
        terraceDetailService.insertTerraceDetail(terraceDetail);
        return toAjax(useTermsService.sysEditMoney(nowValue,varietyValue));
    }
}
